package com.study.mike.common.utils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.*;

/**
 * @author kh
 */
public class CookieUtil {
    private final static String COOKIE_NAME = "officematew";
    private final static String COOKIE_DOMAIN = ".office.com";
    private final static String COOKIE_VALUE = "XOfEnmezblk59%2BS4J95ULmIH8BMX4n%2F%2F384T7yOMG4i%2FlwYkZ7xQJDwbH38Iss23gqSC%2F2BnPZYfMUdKE9e8e3z3Sxaki2ckCWTD4ZU0hzzdm7qOr9%2BwYX0vcyWBikG89ON66ZVH8iQqfTAwudLl1xTls3pyixV6Skkc2kvJVqO009n5lVScqeW85028bvCX%2FND%2BDUPBrB90MXcd%2FjemEsJXjG728TGwnugzYe9qYtNa7x87aNmO%2FaHdPZcTBE5d9TEgzM0ALaGV83s%2FQVvMZxc7qUW8FJ5BNWGAj%2BwiZZg9WZTIYwxDir5%2BBSrgTbc9jbjcnkmL2VhtpXDCjCEUug40ik0tpjF6WVu%2BkLMydlDOovdSXIpXo6pCe2L3%2F%2F0d9TUFjs%2B75Edkd85Cud7PCxc3RCsSXNIFMWQHoGylDQB%2BUMeaRbUsAW8mYpOWVcgXTjvvu1bUIbqzImgQwTY2G6Noqs6lx0oYhe3zX0NuMMo8NGqE%2Bu36XVdeH2P6u79qkofRisjxiUPZcM5GDRfvLNOzrCEkocfbYCiGRp7qn7HVR%2FEG1oQH7NZj7RB7VQHfqulMImHRHdmWVTBxDezY6qd5T%2FMDsD8Xc498alrxf%2FvUWaOdi5wvcMkFi6nM0xuZYRSid5fp1SK8kFr8HQKaEzmt7tv2JC0ohP5hOcbXe7eVGwDHmfhJ98lnNiIQp0IStn7SwJj%2FexhPnrLWrkcNDeDBhg2QgC9Z%2FXahO%2FiwKBWwoVXb3BMEyCILtiJqoasZkSfAlxXkivFYVesuxZ7cY8Q3epXW7Dox0ar%2B%2Fu6%2Fbykx9EKwtxUqaQU7kBAi8qY9DHr7P7DMsjpaQqbsJ3e5EEEq8eaDbEPt%2B6aRJPVp%2B85AwMKVWPITTAWIwkXy6IvUVMEe%2Fy46iV6CdGptBPrkFJHe94v6%2Fd65ZhIFDbK%2F0%2BUSp2AyO6YjkHT0SM14k%2FYcgiukdpnpfcnTwoSs5vi0DEMPgueQjUs1PYV13E%2FqqEvl3FoHrm6W2PPZ9LJoOoYjLfbLACwcce%2BgoClb%2BDf4xy%2FQcg8aG%2FAk1PJq6wnkYHLhSEQ5wqudlyoZDD3dRxCbpjfefRRLAVzLWChsNZg8LrXN55ieZ1F0VT2dw%2FygEDIwPdb24tGxu99y8J77SzNxp9qZKYnU%2FIZJguucQ%2BqpyPmp%2BzUjQ3AOb6SGRImk4vrY8MJceqVZ47bvsvtyBTNetVa0fX0P55VKgAHC9NGqlI74a8IMgDZDBkPKlOe7KdBwBTgf8gFaDKNAwq%2Bppch0u3VDMkwz42X1wirJwMlnLcidrOSOJA7x1a09ames2M3cIJeg3fTvHGtsWWI3hupEamDzbVzgOSmvQeJjoY9wuJ8pxnbb3HU4lF%2BIq1KbLmyQgaf2I0lZ4NCazvkLsbTV30jiCnYLyX%2BlBPDwSdOOgA%3D%3D";

    /**
     * 写cookie
     *
     * @param token    就是sessionid，也就是cookie的值，这个值只要唯一就行了，使用UUID也可以
     * @param response 使用响应对象将cookie写到浏览器上
     */
    public static void writeLoginToken(String token, HttpServletResponse response) {
        Cookie cookie = new Cookie("officematew", COOKIE_VALUE);
        cookie.setPath("/");
        //设置生存时间.0无效，-1永久有效，时间是秒，生存时间设置为1年
        cookie.setMaxAge(36000);
        //设置安全机制
//        cookie.setHttpOnly(true);
        //将cookie写到浏览器上
        response.addCookie(cookie);
    }

    /**
     * 读取cookie
     *
     * @param request
     * @return
     */
    public static String readLoginToken(HttpServletRequest request) {
        //获取cookie
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            //遍历cookie，取出我们自己的cookie，根据name获取
            for (Cookie cookie : cookies) {
                System.out.println("读取cookie cookieName{},cookieValue{}" + cookie.getName() + cookie.getValue());
                //获取自己的
//                if (StrUtil.equals(cookie.getName(), COOKIE_NAME)) {
//                    //获取值
//                }
                return cookie.getValue();
            }
        }
        return null;
    }

    /**
     * 删除cookie
     *
     * @param request
     * @param response
     */
    public static void deleteLoginToken(HttpServletRequest request, HttpServletResponse response) {
//        Cookie[] cookies = request.getCookies();
//        if (cookies != null) {
//            for (Cookie cookie : cookies) {
//                if (StrUtil.equals(cookie.getName(), "officematew")) {
//                    //设置cookie的有效期为
//                    cookie.setMaxAge(0);
//                    cookie.setPath("/");
//                    System.out.println("删除cookie cookieName: {},cookieValue: {}" + cookie.getName() + cookie.getValue());
//                    cookie.setDomain("localhost");
//                    response.addCookie(cookie);
//                    return;
//                }
//            }
//        }
        //假如要删除名称为username的Cookie
        Cookie newCookie = new Cookie("officematew", null);
        //立即删除型
        newCookie.setMaxAge(0);
//        newCookie.setDomain(COOKIE_DOMAIN);//保存cookie的IP地址,则是删除这个IP的cookie
// 项目所有目录均有效，这句很关键，否则不敢保证删除
        newCookie.setPath("/");
        //重新写入，将覆盖之前的
        response.addCookie(newCookie);
    }
}
